package com.hzlh.controller;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzlh.common.util.BaseConstants;
import com.hzlh.domain.po.AccountApplyInfo;
import com.hzlh.log.MethodLog;
import com.hzlh.service.AccountService;

/**
 * 广告主媒体主用户管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("account")
public class AccountController {
	@Autowired
	private AccountService accountService;
 
	/**
	 * @Description: 充值申请账单列表
	 * @param pageIndex 当前页数
	 * @param pageSize 记录条数
	 * @return Map<String,Object>  
	 * @throws
	 */
	@MethodLog(key="ACCOUNT.APPLYLIST")
/*	@RequestMapping(value="/applyList",method=RequestMethod.POST)*/
	@RequestMapping("/applyList")
	@ResponseBody
	@RequiresPermissions("account:applyList")
	public Map<String,Object> page(int pageIndex,int pageSize,String telphone,String email,String username,String linkman){
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("start", pageIndex);
			map.put("end", pageSize);
			map.put("telphone", telphone);
			map.put("email", email);
			if(username!=null&&!"".equals(username))
				map.put("username", URLDecoder.decode(username, "UTF-8"));
			map.put("status", "0");
			if(linkman!=null&&!"".equals(linkman))
				map.put("linkman", URLDecoder.decode(linkman, "UTF-8"));
			return accountService.applyPage(map);
		} catch (Exception e) {
			Map<String, Object> remap = new HashMap<String,Object>();
			remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, 0);
			remap.put(BaseConstants.MCPP_HC_PAGE_DATA, null);
			return null;
		}
		
	}
	
	/**
	 * 充值记录
	 * @param pageIndex
	 * @param pageSize
	 * @param telphone
	 * @param email
	 * @param username
	 * @param linkman
	 * @param status
	 * @return
	 */
//	@MethodLog(key="ACCOUNT.APPLYLIST")
	/*	@RequestMapping(value="/applyList",method=RequestMethod.POST)*/
		@RequestMapping("/recordList")
		@ResponseBody
		@RequiresPermissions("account:recordList")
		public Map<String,Object> recordPage(int pageIndex,int pageSize,String telphone,String email,String username,String linkman,String status){
			try {
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("start", pageIndex);
				map.put("end", pageSize);
				map.put("telphone", telphone);
				map.put("email", email);
				if(username!=null&&!"".equals(username))
					map.put("username", URLDecoder.decode(username, "UTF-8"));
				if(status!=null && !"-1".equals(status))
					map.put("status", status);
				if(linkman!=null&&!"".equals(linkman))
					map.put("linkman", URLDecoder.decode(linkman, "UTF-8"));
			
				
				return accountService.applyPage(map);
			} catch (Exception e) {
				Map<String, Object> remap = new HashMap<String,Object>();
				remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, 0);
				remap.put(BaseConstants.MCPP_HC_PAGE_DATA, null);
				return null;
			}
			
		}
	
	/**
	 * 充值申请详情信息查看
	 * @param model
	 * @param id
	 * @return
	 */
//	@MethodLog(key="ACCOUNT.DETAIL")
	@RequestMapping("/detail")
	public String detail(Model model, String id){
		AccountApplyInfo po = accountService.getApplyDetail(id);
		model.addAttribute("user", po);
		return "account/detail";
	}
	
	
	/**
	 * 充值申请详情信息查看
	 * @param model
	 * @param id
	 * @return
	 */
//	@MethodLog(key="ACCOUNT.DETAIL")
	@RequestMapping("/detail_false")
	public String detail_false(Model model, String id){
		AccountApplyInfo po = accountService.getApplyDetail(id);
		model.addAttribute("user", po);
		return "account/detail_false";
	}
	
	/**
	 * @Description: 不予充值
	 * @param id
	 * @return boolean  
	 * @throws
	 */
//	@MethodLog(key="ACCOUNT.REJECT")
	@RequestMapping(value="/reject",method=RequestMethod.POST)
	@ResponseBody
	public boolean reject(String id,String status,String reason){
		Map<String, String> map = new HashMap<String,String>();
		map.put("id", id);
		map.put("status", status);
		map.put("reason", reason);
		return accountService.reject(map);
	}
	
	/**
	 * @Description: 充值
	 * @param id
	 * @return boolean  
	 * @throws
	 */
//	@MethodLog(key="ACCOUNT.PAY")
	@RequestMapping(value="/pay",method=RequestMethod.POST)
	@ResponseBody
	public boolean pay(String id,String status,String paytype,String repay_date,String accounts){
		boolean result = false;
		try {
			Map<String, String> map = new HashMap<String,String>();
			map.put("id", id);
			map.put("status", status);
			map.put("paytype", paytype);
			map.put("repay_date", repay_date);
			map.put("accounts", accounts);
			accountService.pay(map);		//修改充值记录状态为成功
			accountService.updateAccountInfo(map);		//修改账户金额
//			accountService.pay(map);		//插入一条金额记录
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * @Description: 还现
	 * @param id
	 * @return boolean  
	 * @throws
	 */
//	@MethodLog(key="ACCOUNT.REJECT")
	@RequestMapping(value="/repay",method=RequestMethod.POST)
	@ResponseBody
	public boolean repay(String id,String status,String repay_account, String repay_name,String repay_bank,String repay_bank_name,String repay_serial){
		Map<String, String> map = new HashMap<String,String>();
		map.put("id", id);
	
		map.put("repay_account", repay_account);
		map.put("repay_name", repay_name);
		map.put("repay_bank", repay_bank);
		map.put("repay_bank_name", repay_bank_name);
		map.put("repay_serial", repay_serial);
		map.put("status", status);
		return accountService.repay(map);
	}
	/**
	 * 离线充值查询用户名称
	 * @param po
	 * @return String
	 * @author sunguiyang 
	 * @author 2016/08/18 
	 */
	@RequestMapping("/queryusername")
	@ResponseBody
	public String queryusername(Model model,String emailurl){
		AccountApplyInfo po = accountService.getqueryusername(emailurl);
		if(po==null){
			model.addAttribute("query","此平台账号没有客户名");
			return "此平台账号没有客户名";
		}
		model.addAttribute("query", po);
		return po.getId()+","+po.getUsername()+","+po.getLinkname()+","+po.getTelphone();
	}
	/**
	 * 离线充值
	 * @param po
	 * @return boolean
	 * @author sunguiyang 
	 * @author 2016/08/18 
	 */
	@RequestMapping(value="/offOperate",method=RequestMethod.POST)
	@ResponseBody
	public boolean offOperate(AccountApplyInfo po ,String status){
		boolean flage =false;
		Map<String, String> map = new HashMap<String,String>();
		if(po.getPay_type().equals("2")){
			map.put("pay_type", "透现");	
		}
		if( po.getRepay_time().equals("")){
			
			map.put("repay_time", null);
		}else{
			
			map.put("repay_time", po.getRepay_time());
		}
		map.put("user_id", po.getUser_id());
		map.put("account", po.getAccount());	
		map.put("repay_account", po.getRepay_account());
		map.put("explanation", po.getExplanation());	
		map.put("repay_bank_name", po.getRepay_bank_name());
		map.put("repay_serial", po.getRepay_serial());	
		map.put("repay_link_name", po.getRepay_link_name());	
		map.put("repay_telphone", po.getRepay_telphone());	
		//插入广告主线下充值申请信息表
		flage =accountService.offOperateinsert(map);
		//更新广告主扩展表-账户信息表中的金额
		
		Map<String, String> map1 = new HashMap<String,String>();
		map1.put("user_id", po.getUser_id());
		map1.put("accounts", po.getAccount());
		accountService.updateAccountInfo1(map1);
		return flage;
	}
	
}